package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.BaseProdInfoMapper;
import cc.rengu.igas.bomp.common.dao.ProdInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BaseProdInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ProdInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BaseProdInfo;
import cc.rengu.igas.bomp.common.entity.ProdInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.BaseProdInfoBean;
import cc.rengu.igas.bomp.facade.bean.ProdInfoBean;
import cc.rengu.igas.bomp.facade.request.ProdInfoListQueryRequest;
import cc.rengu.igas.bomp.facade.response.ProdInfoListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 标准产品列表查询服务
 * Created by 王成 on 2020/3/9
 */
public class ProdInfoListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ProdInfoListQueryRequest prodInfoListQueryRequest = new ProdInfoListQueryRequest();
        ConvertUtil.convertOutput(prodInfoListQueryRequest);
        return prodInfoListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ProdInfoListQueryRequest prodInfoListQueryRequest = (ProdInfoListQueryRequest) request;
        ProdInfoListQueryResponse prodInfoListQueryResponse = new ProdInfoListQueryResponse();
        BeanUtil.beanCopy(prodInfoListQueryRequest, prodInfoListQueryResponse);
        String instId = prodInfoListQueryRequest.getHeader().getInstId();
        /* 根据法人行查询对应的标准产品信息 */
        int pageIndex = prodInfoListQueryRequest.getPageIndex();
        int pageSize = prodInfoListQueryRequest.getPageSize();

        ProdInfoMapper prodInfoMapper = new ProdInfoMapperImpl();
        if (-1 == pageIndex || -1 == pageSize) {
            List<ProdInfo> prodInfoList = prodInfoMapper.selectProdInfoByInstId(prodInfoListQueryRequest.getHeader().getInstId());
            if (CollectionUtils.isEmpty(prodInfoList)) {
                rglog.error("法人行<{}>还未配置标准产品信息!", prodInfoListQueryRequest.getHeader().getInstId());
                prodInfoListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                prodInfoListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
            } else {
                /* 按照创建时间降序排序 */
                prodInfoList = prodInfoList.stream().sorted(Comparator.comparing(ProdInfo::getCreateTime)).collect(Collectors.toList());
                Collections.reverse(prodInfoList);
                //过滤前总记录数
                int totalRows = prodInfoList.size();
                Map<String, ProdInfoBean> map = new HashMap<>();
                for (ProdInfo prodInfo : prodInfoList) {
                    String key = prodInfo.getProdCode();
                    if (map.containsKey(prodInfo.getProdCode())) {
                        ProdInfoBean prodInfoBean = map.get(key);
                        BaseProdInfoBean baseProdInfoBean = new BaseProdInfoBean();
                        BeanUtil.beanCopy(prodInfo, baseProdInfoBean);
                        BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
                        BaseProdInfo baseProdInfo = baseProdInfoMapper.selectBaseProdInfoByPrimaryKey(instId, prodInfo.getBaseProdCode());
                        BeanUtil.beanCopy(baseProdInfo, baseProdInfoBean);
                        prodInfoBean.getBaseProdInfoList().add(baseProdInfoBean);
                    } else {
                        ProdInfoBean prodInfoBean = new ProdInfoBean();
                        BeanUtil.beanCopy(prodInfo, prodInfoBean);
                        List<BaseProdInfoBean> baseProdInfoBeanList = new ArrayList<>();
                        BaseProdInfoBean baseProdInfoBean = new BaseProdInfoBean();
                        BeanUtil.beanCopy(prodInfo, baseProdInfoBean);
                        baseProdInfoBean.setBaseProdCode(prodInfo.getBaseProdCode());
                        BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
                        BaseProdInfo baseProdInfo = baseProdInfoMapper.selectBaseProdInfoByPrimaryKey(instId, prodInfo.getBaseProdCode());
                        BeanUtil.beanCopy(baseProdInfo, baseProdInfoBean);

                        baseProdInfoBeanList.add(baseProdInfoBean);
                        prodInfoBean.setBaseProdInfoList(baseProdInfoBeanList);
                        map.put(key, prodInfoBean);
                    }
                }

                List<ProdInfoBean> prodInfoBeans = new ArrayList<>(map.values());

                /* 查询结果赋值 */
                prodInfoListQueryResponse.setTotalRows(totalRows);//总记录数
                prodInfoListQueryResponse.setProdInfoList(prodInfoBeans);
            }
        } else {

            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            PageResult pageResult = prodInfoMapper.selectProdInfoByInstId(instId, pageInfo);

            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                rglog.error("根据查询条件查询代理机构信息 pageResult 为null !");
                throw new BizException(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条代理机构信息!");
                prodInfoListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                prodInfoListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
            } else {
                List<ProdInfo> prodInfoList = new ArrayList<>();
                for (Object object : pageResult.getResult()) {
                    prodInfoList.add((ProdInfo) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());
                /* 按照创建时间降序排序 */
                prodInfoList = prodInfoList.stream().sorted(Comparator.comparing(ProdInfo::getCreateTime)).collect(Collectors.toList());
                Collections.reverse(prodInfoList);
                Map<String, ProdInfoBean> map = new HashMap<>();

                for (ProdInfo prodInfo : prodInfoList) {
                    String key = prodInfo.getProdCode();
                    if (map.containsKey(prodInfo.getProdCode())) {
                        ProdInfoBean prodInfoBean = map.get(key);
                        BaseProdInfoBean baseProdInfoBean = new BaseProdInfoBean();
                        BeanUtil.beanCopy(baseProdInfoBean, prodInfo);
                        prodInfoBean.getBaseProdInfoList().add(baseProdInfoBean);
                    } else {
                        ProdInfoBean prodInfoBean = new ProdInfoBean();
                        BeanUtil.beanCopy(prodInfo, prodInfoBean);
                        List<BaseProdInfoBean> baseProdInfoBeanList = new ArrayList<>();
                        BaseProdInfoBean baseProdInfoBean = new BaseProdInfoBean();
                        BeanUtil.beanCopy(baseProdInfoBean, prodInfo);
                        baseProdInfoBeanList.add(baseProdInfoBean);
                        prodInfoBean.setBaseProdInfoList(baseProdInfoBeanList);
                        map.put(key, prodInfoBean);
                    }
                }

                List<ProdInfoBean> prodInfoBeans = new ArrayList<>(map.values());
                /* 查询结果赋值 */
                prodInfoListQueryResponse.setProdInfoList(prodInfoBeans);
                prodInfoListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                prodInfoListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());

            }

        }


        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(prodInfoListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ProdInfoListQueryResponse prodInfoListQueryResponse = (ProdInfoListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(prodInfoListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param prodInfo      数据库表结构对象
     * @param prodInfoBeans 接口对象
     */
    private void convProdInfo(ProdInfo prodInfo, List<ProdInfoBean> prodInfoBeans) {
        ProdInfoBean prodInfoBean = new ProdInfoBean();
        BeanUtil.beanCopy(prodInfo, prodInfoBean);
        prodInfoBeans.add(prodInfoBean);
    }
}
